<? include_once("../../consoles/config.php");?>

<?
//请确保表格存在，并且公众号数据在nid=1的位置
$wechat_detail=select_one_line_all_items($table_prefix."wechat_official","nid = 1");


print_r($wechat_detail);




//配置参数的数组
$CONF =  array(
    '__APPID__' =>$wechat_detail['appid'],
    '__SERECT__' =>$wechat_detail['appsecret']
);




echo "<br/>".$wechat_detail['name']."token时限".$wechat_detail['token_expire']."<br/>";



//判断token是否已经过期
if($wechat_detail['token_expire']<date("YmdHis"))
{

    echo $wechat_detail['token_expire'];
    echo "<br/>已经超时<br/>";
    echo date("YmdHis");




//过期了就要重新拿,没过期就直接提取可以了。
    $gettokenUrl  =  "https://api.weixin.qq.com/cgi-bin/token".
        "?grant_type=client_credential".
        "&appid=" . $CONF['__APPID__'].
        "&secret=".$CONF['__SERECT__']
    ;


    $get_json=file_get_contents($gettokenUrl);


    echo "<br/>".$get_json."<br/>";


    $access_token      = substr($get_json, 17, 136);
    $token['ai_token'] = substr($get_json, 17, 136);


    echo "现在时间<br/>".$token_expire=date("YmdHis");

    $token_expire            =date("YmdHis",strtotime('+1 hour'));
    echo $token['ai_token_expire']=date("YmdHis",strtotime('+1 hour'));
    echo  "<br/>";


    $update_token=update_date($table_prefix."wechat_official",$token,"nid",'1');
    $query = mysql_query($update_token) or die ($update_token);
}


//token未过期
else
{
    echo "<br/>未超时，直接使用<br/>";
    $access_token=$wechat_detail['token'];
    echo "<br/>";

}

echo "<br/>".$wechat_detail['name']."token:".$access_token."<br/>";



//最终输出的是$access_token



?> 


 